<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?>
    	<?php if(isset($breadcrumb['href']) && $breadcrumb['href'] != '' && $breadcrumb['href'] != '#') {?>
	    	<a href="<?php echo $breadcrumb['href']; ?>">
	    		<?php echo $breadcrumb['text']; ?>
	    	</a>
    	<?php }else{?>
    		<?php echo $breadcrumb['text']; ?>
    	<?php }?>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/product.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a onclick="location = '<?php echo $cancel; ?>';" class="button"><?php echo $button_cancel; ?></a></div>
    </div>
    <div class="content">
      <div id="tabs" class="htabs">
      	<a href="#tab-general"><?php echo $tab_general; ?></a>
      	<a href="#tab-restaurant"><?php echo $tab_restaurant; ?></a>
      </div>
      <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
        <div id="tab-general">
            <table class="form">
              <tr>
                <td><span class="required">*</span> <?php echo $entry_name; ?></td>
                <td><input type="text" name="name" value="<?php echo $name; ?>" size="60" />
	              <?php if ($error_name) { ?>
	              <span class="error"><?php echo $error_name; ?></span>
	              <?php } ?></td>
              </tr>
              <tr>
                <td><span class="required">*</span> <?php echo $entry_directory; ?></td>
                <td><input type="text" name="directory" value="<?php echo $directory; ?>" />
	              <?php if ($error_directory) { ?>
	              <span class="error"><?php echo $error_directory; ?></span>
	              <?php } ?>
	              <?php if ($error_directory_not_exist) { ?>
	              <span class="error"><?php echo $error_directory_not_exist; ?></span>
	              <?php } ?>
				</td>
              </tr>
              <tr>
                <td><?php echo $entry_image; ?></td>
                <td><img src="<?php echo $image; ?>" alt="" id="thumb" /></td>
              </tr>
              <tr>
	              <td><?php echo $entry_type; ?></td>
	              <td><select name="type">
	                  <?php if ($type) { ?>
	                  <option value="1" selected="selected"><?php echo $text_private; ?></option>
	                  <option value="0"><?php echo $text_public; ?></option>
	                  <?php } else { ?>
	                  <option value="1"><?php echo $text_private; ?></option>
	                  <option value="0" selected="selected"><?php echo $text_public; ?></option>
	                  <?php } ?>
	                </select></td>
              </tr>
              <tr>
	              <td><?php echo $entry_status; ?></td>
	              <td><select name="status">
	                  <?php if ($status) { ?>
	                  <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
	                  <option value="0"><?php echo $text_disabled; ?></option>
	                  <?php } else { ?>
	                  <option value="1"><?php echo $text_enabled; ?></option>
	                  <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
	                  <?php } ?>
	                </select></td>
              </tr>
            </table>
        </div>
        <div id="tab-restaurant">
          <table class="form">
            <tr>
                <td><?php echo $entry_restaurant_name; ?></td>
                <td>
                	<input type="text" name="restaurant_name" value="" size="60" />
				</td>
            </tr>
             <tr>
                <td colspan="2">
                	<table class="list">
			          <thead>
			            <tr>
			              <td class="left"><?php echo $column_restaurant_name; ?></td>
			              <td class="left"><?php echo $column_url; ?></td>
			              <td class="center"><?php echo $column_action; ?></td>
			            </tr>
			          </thead>
			          <tbody>
			         	<?php $restaurant_row = 0; ?>
			         	<?php 
			         	foreach ($restaurants as $restaurant) { 
			         	?>
			         	<tr id="restaurant_row<?php echo $restaurant_row;?>">
							<td class="left">
								<input type="hidden" name="restaurant_id[]" value="<?php echo $restaurant['restaurant_id'];?>" />
								<input type="hidden" name="restaurants[<?php echo $restaurant_row;?>][restaurant_id]" value="<?php echo $restaurant['restaurant_id']?>" />
								<input type="hidden" name="restaurants[<?php echo $restaurant_row;?>][name]" value="<?php echo $restaurant['name'];?>" />
								<input type="hidden" name="restaurants[<?php echo $restaurant_row;?>][url]" value="<?php echo $restaurant['url'];?>" />
								<?php echo $restaurant['name'];?>
							</td>
							<td class="left">
								<?php echo $restaurant['url'];?>
							</td>
							<td class="center">
								<?php 
								if(!$restaurant['inUse']) {
								?>
								<a onclick="$('#restaurant_row<?php echo $restaurant_row;?>').remove();" class="button"><?php echo $button_delete; ?></a>
								<?php 
								} else {
									echo $text_in_use;
								}
								?>
							</td>
						</tr>
			         	<?php 
				         	$restaurant_row++;
			         	} 
			         	?>
			         	
			            <tr id="dummy-row" style="visibility:hidden">
			              <td class="left">aaaaa</td>
			              <td class="left">bbbbb</td>
			              <td class="center" style = "width:100px">
			              </td>
			            </tr>
			          </tbody>
			        </table>
                </td>
             </tr>
          </table>
        </div>
      </form>
    </div>
  </div>
</div>

<script type="text/javascript"><!--	
var restaurant_row = <?php echo $restaurant_row; ?>;

$('input[name=\'restaurant_name\']').autocomplete({
	delay: 0,
	source: function(request, response) {
		$.ajax({
			url: 'index.php?route=restaurant/autocomplete/restaurant&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request.term),
			dataType: 'json',
			data: {
				ignore_restaurant_ids : function(){
					var restaurantIdElements = $('input[name^=restaurant_id]');
					
					// create an new object with properties id and price
					var restaurantIds = new Array();
					
					// loop over the price input elements and place the value in the postData object
					var i = 0;
					$.each(restaurantIdElements, function(index, el) {
						// push the value in the price array
						restaurantIds[i++] = $(el).val();
					});
				
					return restaurantIds;
				}
			},
			success: function(json) {
				response($.map(json, function(item) {
					return {
						category: "<?php echo $text_select; ?>",
						label: item.name,
						value: item.restaurant_id,
						url: item.url
					};
				}));
			}
		});
	}, 
	select: function(event, ui) {
		html   = '<tr id="restaurant_row' + restaurant_row + '">';
		html  += '	<td class="left">';
		html  += '		<input type="hidden" name="restaurant_id[]" value="' + ui.item.value + '" />';
		html  += '		<input type="hidden" name="restaurants[' + restaurant_row + '][restaurant_id]" value="' + ui.item.value + '" />';
		html  += '		<input type="hidden" name="restaurants[' + restaurant_row + '][name]" value="' + ui.item.label + '" />';
		html  += '		<input type="hidden" name="restaurants[' + restaurant_row + '][url]" value="' + ui.item.url + '" />';
		html  += '			' + ui.item.label;
		html  += '	</td>';
		html  += '	<td class="left">';
		html  += '			' + ui.item.url;
		html  += '	</td>';
		html  += '	<td class="center">';
		html  += '		<a onclick="$(\'#restaurant_row' + restaurant_row + '\').remove();" class="button"><?php echo $button_delete; ?></a>';
		html  += '	</td>';
		html  += '</tr>';

		$('#dummy-row').before(html);

		$('input[name="restaurant_name"]').val("");

		restaurant_row++;
			
		return false;
	}
});

$('input[name=\'directory\']').change(function() {
	// Check if image name not equal to directory name then reset the image to no image
	var image = $('#thumb').attr("src").toString().match(/.*\/(.+?)\./);
	if($(this).val() != image){
		$('#thumb').attr("src", "<?php echo $no_image; ?>");
	}
});

$('input[name=\'directory\']').autocomplete({
	delay: 0,
	source: function(request, response) {
		$.ajax({
			url: 'index.php?route=setting/autocomplete/templateDirectory&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request.term),
			dataType: 'json',
			success: function(json) {
				response($.map(json, function(item) {
					return {
						category: "<?php echo $text_select; ?>",
						label: item.directory,
						value: item.directory,
						image: item.image
					};
				}));
			}
		});
	}, 
	select: function(event, ui) {
		$('input[name=\'directory\']').val(ui.item.label);
		$('#thumb').attr("src", ui.item.image);
		
		return false;
	}
});
//--></script> 
<script type="text/javascript"><!--
$('#tabs a').tabs(); 
$('#languages a').tabs(); 
$('#vtab-option a').tabs();
//--></script> 
<?php echo $footer; ?>